<?php

namespace app\admin\controller;
use app\base\controller\Base;
use think\Request;
use think\Cache;
use think\Config;
use think\Session;


class Site extends Base
{
    public  $model;

    public function __construct()
    {
        parent::__construct();
        $this->model = model("Site");
        $this->whereModel = ["id"=>1];
    }


    /**
     *   显示站点配置显示
     */
    public function index(){

          $res =   $this->model->where($this->whereModel)->find();
          if(empty($res)){
          return       failJson("暂无该站点信息");
          }

          return sucessJson("查询成功",$res);

    }


    //保存站点信息
    public function save(){



        $count = $this->model->where($this->whereModel)->count();
        if($count>0){
            $result = 1;
        }else{
                $result = 0;
        }
        $data = [];


        $this->fileDelwith("app_logo",$data);
        $this->fileDelwith("admin_logo",$data);
        $this->fileDelwith("main_logo_zhuye",$data);
        $this->fileDelwith("main_thumb",$data);
        $this->fileDelwith("qcode_url",$data);

        $this->fiedwith("confirmation",$data);
        $this->fiedwith("company_name",$data);
        $this->fiedwith("tel",$data);
        $this->fiedwith("special_version",$data);
        $this->fiedwith("app_title",$data);
        $this->fiedwith("admin_copywriting_title",$data);
        $this->fiedwith("admin_copywriting_content",$data);
        $this->fiedwith("h5_url",$data);
        $this->fiedwith("time_out_fail",$data);
        $this->fiedwith("qcode_cz_add",$data);
        $this->fiedwith("qcode_cz_zy",$data);
        $this->fiedwith("qcode_cz",$data);
        $this->fiedwith("qcode_cz_bankno",$data);
        $this->fiedwith("qcode_cz_name",$data);
        if(empty($data)){
               return  failJson("请提交数据");
        }
        if($result){
            $res = $this->model->save($data,$this->whereModel);
        }else{
                $res = $this->model->save(array_merge($data,$this->whereModel));
        }

        Cache::set("SiteCofig",$data);



        if(!empty($res)){
            return  sucessJson("修改成功");
        }
        return failJson("修改失败");

    }
    private function fileDelwith($filed,&$data){
        if(!isset($this->$filed)){
            return false;
        }
        $base64_image_content = $this->$filed;
        if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){
            //图片后缀
            $type = $result[2];
            //保存位置--图片名
            $image_name=date('His').str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT).".".$type;
            $image_url = '/uploads/image/'.date('Ymd').'/'.$image_name;
            if(!is_dir(dirname('.'.$image_url))){
                mkdir(dirname('.'.$image_url),0777,true);
                chmod(dirname('.'.$image_url), 0777);
//            umask($oldumask);
            }
            //解码
            $decode=base64_decode(str_replace($result[1], '', $base64_image_content));
            if (file_put_contents('.'.$image_url, $decode)){
                $data[$filed]=$image_url;
            }
        }


    }


    //处理上传的图片
   private function fileDelwithBack($filed,&$data){

        $file = request()->file($filed);//logo的处理
        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $uploadPath = ROOT_PATH . 'public' . DS . 'uploads/';
            $info = $file->move($uploadPath);
            if($info){
                $data[$filed] = $uploadPath.$info->getSaveName();
            }


        }
    }

    //字段的处理
    private function fiedwith($filed,&$data){
                if(isset($this->$filed)){
                     $data[$filed] = $this->$filed;
                }

    }

}
